package fausto.cycles 
{
	import ben.ImgTools;
	import ben.SomeTools;
	import com.greensock.TweenMax;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	/**
	 * ...
	 * @author Ben.
	 */
	public class CyclesFull extends Sprite
	{
		private var _mainSp:Sprite
		private var _prevSp:Sprite
		private var _nextSp:Sprite
		
		private var ww:int = 440;
		private var hh:int = 440;
		
		private var _dd:XML
	
		private var _sp:Sprite
		
		private var _current:int
		private var _it:ImgTools;
		
		public var _nextBtn:Sprite
		public var _prevBtn:Sprite
		
		public function CyclesFull(_d:XML, _i:int):void 
		{
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
			
			trace(_d);
			trace(_d.photo.length());
			
			_dd = _d;
			_current = _i;
			
			// MAIN IMG()
			_mainSp = new Sprite();
			loadImg(_mainSp, 320, _current);
		
			// PREV IMG()
			var prv:int = (_current - 1) < 0 ? _d.photo.length() - 1 : _current - 1;
			_prevSp = new Sprite();
			loadImg(_prevSp, -2000, prv, 0.3, 0.3);
			
			// NEXT IMG()
			var nxt:int = (_current + 1) == _d.photo.length() ? 0 : _current +1;
			_nextSp = new Sprite();
			loadImg(_nextSp, 2000, nxt, 0.3, 0.3);
			
			// UI()
			disableIt();
			
			_prevBtn.buttonMode = _nextBtn.buttonMode = true;
			
			_nextBtn.addEventListener(MouseEvent.CLICK, onNext);
			_prevBtn.addEventListener(MouseEvent.CLICK, onPrev);
		}
		
		private function disableIt():void
		{
			_mainSp.mouseChildren = _prevSp.mouseChildren = _nextSp.mouseChildren = false;
			_mainSp.mouseEnabled = _prevSp.mouseEnabled = _nextSp.mouseEnabled = false;
		}
		
		private function loadImg(_sp:Sprite, _x:Number, _i:int, _a:Number = 1, _s:Number = 1):void
		{
			_sp.x = _x;
			_sp.y = 220;
			addChild(_sp);
			
			_it = new ImgTools();
			_it.ImgFitSize("img/" + _dd.photo[_i].full, _sp, ww, hh);
			_it.addEventListener("LOADED", onResized);
			_sp.alpha = _a;
			_sp.scaleX = _sp.scaleY = _s;
		}
		
		private function onAdded(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAdded);
			stage.addEventListener(Event.RESIZE, onResized);
		}
		
		private function onResized(e:Event):void 
		{
			if (_prevSp)
			{
				_prevSp.x = -(stage.stageWidth - 640) / 2;
			}
			
			if (_nextSp)
			{
				_nextSp.x = 640+ (stage.stageWidth - 640) / 2;
			}
		}
		
		public function killBill():void
		{
			if (_mainSp)
			{
				removeChild(_mainSp);
			}
			if (_prevSp)
			{
				removeChild(_prevSp);
			}
			if (_nextSp)
			{
				removeChild(_nextSp);
			}
		}
		
		private function onPrev(e:MouseEvent):void 
		{
			if (!_prevSp)
			{
				return;
			}
			removeChild(_nextSp);
				
			TweenMax.to(_mainSp, 0.6, { x: 640+(stage.stageWidth - 640) / 2, alpha:0.3, scaleX:0.3, scaleY:0.3 } );
			TweenMax.to(_prevSp, 0.6, { x: 320, alpha:1, scaleX:1, scaleY:1 , onComplete:reFactorPrev} );
		}
		
		private function onNext(e:MouseEvent):void 
		{
			if (!_nextSp)
			{
				return;
			}
			removeChild(_prevSp);
			
			TweenMax.to(_mainSp, 0.6, { x: -(stage.stageWidth - 640) / 2, alpha:0.3, scaleX:0.3, scaleY:0.3} );
			TweenMax.to(_nextSp, 0.6, { x: 320, alpha:1, scaleX:1, scaleY:1, onComplete:reFactorNext } );
		}
		
		private function reFactorNext():void 
		{
			_prevSp = _mainSp;
			_mainSp = _nextSp;
			
			_nextSp = null;
			
			_current++;
			
			if (_current == _dd.photo.length())
			{
				_current = 0;
			}
			
			// LOAD NEXT()
			var nxt:int = (_current + 1) == _dd.photo.length() ? 0 : _current +1;
			_nextSp = new Sprite();
			loadImg(_nextSp, 2000, nxt, 0.3, 0.3);
			
			disableIt();
		}
		
		private function reFactorPrev():void 
		{
			_nextSp = _mainSp;
			_mainSp = _prevSp;
			
			_prevSp = null;
			
			_current--;
			
			if (_current == -1)
			{
				_current = _dd.photo.length()-1;
			}
			
			// LOAD PREV()
			var prv:int = (_current - 1) < 0 ? _dd.photo.length() - 1 : _current - 1;
			_prevSp = new Sprite();
			loadImg(_prevSp, -2000, prv, 0.3, 0.3);
			
			disableIt();
		}
	}

}